/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.wzm.memento.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import it.wzm.memento.object.Mobject;
import it.wzm.memento.log.Log;

/**
 *
 * @author Leonardo Celati
 */
public class DataXchange {
    
    enum Status {
        DEAD,
        STARTUP,
        OPERATION,
        SHUTDOWN,
    }
    
    
    
    private static DataXchange dx = null;
    private static Connection conn=null;
    private Log l = Log.getLog();
    
    /**
     * We are implementing the singleton instance
     */
    private DataXchange() {
    }
    
    /**
     * This is the only accessor for creating a DataXchange instance
     * @return
     */
    public DataXchange newInstance() throws DataXchangeException {
        
        // just a basic check
        if (this.dx != null) {
            
            // checking if conn is null
            if (dx.conn != null) {
                throw new DataXchangeException("Connection is null");
            }
            
            l.debug("We already have a connection " + conn);
            return this.dx;
            
        } else {
            try {
                
                l.info("First time opening connection");
                dx = new DataXchange();
                dx.startup();
                return dx; 
            
            } catch (ClassNotFoundException e) {
                throw new DataXchangeException("Driver error", e);
            } catch (SQLException e) {
                throw new DataXchangeException("Some sort of sql initialization fail", e);
            }
        }
        
    }
    
    /**
     * 
     * @throws java.lang.ClassNotFoundException
     * @throws java.sql.SQLException
     */
    private void startup() throws ClassNotFoundException,SQLException {

            l.info("Load driver");
            Class.forName("org.hsqldb.jdbcDriver");
            l.info("Open the database connection");
            conn = DriverManager.getConnection("jdbc:hsqldb:file:data/repository","madmin","+madmin+");
            l.info(conn);            
    }
    
    /**
     * Close connection
     * @throws it.wzm.memento.sql.DataXchangeException
     */
    public void shutdown() throws DataXchangeException {
        try {            
            
            l.info("Shutdown request has been made");
            
            // performing basic check, for dx and conn already null
            if (dx == null || conn == null) {
                throw new DataXchangeException("null");
            }
                        
            // performing closing command
            conn.createStatement().execute("SHUTDOWN");
            conn.close();
            l.info("Shutdown ok");
        } catch (SQLException e) {
            throw new DataXchangeException("Cannot shutdown DataXchange", e);
        }        
    }
    
    
    
    public synchronized void put(Mobject mo) throws DataXchangeException {
        
        l.debug("put " + mo.getName());
        
        // check connection
        if (conn == null) {
            throw new DataXchangeException("Connection null");
        }
        
        l.debug("Finished");
    }
    
    
    public synchronized Mobject get() throws DataXchangeException {
        
        if (conn==null) {
            throw new DataXchangeException("Connection null");
        }
        
        l.debug("Finished");
        return null;
    }
    
    

}
